package com.jbm.jbmsupplier.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.jbm.core.iface.IAcross;
import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.rxjavanet.ModeRxjavaNet;
import com.jbm.jbmsupplier.shared.SharedPreferActivity;
import com.jbm.jbmsupplier.utlis.ExitActivity;
import com.jbm.jbmsupplier.view.DoubleButtonDialog;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


/**
 * Created by 家帮忙 on 2017/4/10.
 */

public abstract class AllActivityApp extends ActionBarActivity {
    FrameLayout frameid;
    public abstract int setContentView();
    public abstract void initIalizeView();
    private ExitBroadcase exite=null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.allappinitialize_lzyout);
        frameid= (FrameLayout) findViewById(R.id.frameid);
        frameid.addView(View.inflate(this, setContentView(), null));
        if (exite==null){
        exite =new ExitBroadcase();
          registerReceiver(exite,new IntentFilter("EXIT"));}
        initIalizeView();
    }
    class ExitBroadcase extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
                setExitUser("您的用户已在其他处登录" + "\n" + "请检查",null,1);

        }
    }
    //一些提示的时候调用
    public void hint(String name) {
        final DoubleButtonDialog dialog = new DoubleButtonDialog(this, name);
        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setisVisibility();

    }

    //提示用户，将有别的操作时调用
    public void hint(String name, final Handler handler, final int exit) {
        final DoubleButtonDialog dialog = new DoubleButtonDialog(AllActivityApp.this, name);
        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                handler.sendMessage(handler.obtainMessage(exit));
            }
        });
        dialog.setisVisibility();
    }
    public void hints(String name, final Handler handler, final int exit) {
        final DoubleButtonDialog dialog = new DoubleButtonDialog(AllActivityApp.this, name);
        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                handler.sendMessage(handler.obtainMessage(exit));
            }
        });
        dialog.getnobtn().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    //用户退出时使用
    public void exit(String name) {
        final DoubleButtonDialog dialog = new DoubleButtonDialog(AllActivityApp.this, name);
        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
                ExitActivity.getInstance().exitActivity();
                dialog.dismiss();

            }
        });
        dialog.getnobtn().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    //在用户被挤掉和修改密码的时候调用
    public void setExitUser(String name, final Handler handler, final int tag) {

        final DoubleButtonDialog dialog = new DoubleButtonDialog(AllActivityApp.this, name);
        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SharedPreferActivity(AllActivityApp.this).setExitLogins();
                startActivity(new Intent(AllActivityApp.this, LoginActivity.class));
                ExitActivity.getInstance().exitActivity();
                    Observable.create(new Observable.OnSubscribe<IAcross>() {
                    @Override
                    public void call(Subscriber<? super IAcross> subscriber) {
                        IAcross exit = ModeRxjavaNet.Companion.getInstantMode().getOhInter().Logout();
                        subscriber.onNext(exit);
                    }
                }).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(action);
                dialog.dismiss();
            }
        });
        dialog.setisVisibility();

    }

    Action1<IAcross>action =new Action1<IAcross>(){

        @Override
        public void call(IAcross iAcross) {
            if (iAcross.m_code==200){
                Log.e("main","退出成功");
            }
        }
    };



    @Override
    protected void onDestroy() {
        super.onDestroy();
                if (exite!=null){
           unregisterReceiver(exite);
                    exite=null;
        }
    }
}
